﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RonYee.Models;
using RonYee.ViewModels;

namespace RonYee.Controllers
{
    public class ShoppingCartController : Controller
    {
        RongeDb storeDB = new RongeDb();
        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            //Set up our ViewModel
            var viewModel = new ShoppingCartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };
            //Return the view
            return View(viewModel);
        }
        //
        //GET: /Store/AddToCart/5
        public ActionResult AddToCart(int id) 
        {
            //Retrieve the book from the database
            var addedBook = storeDB.Books.Single(book => book.BookId == id);

            //Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(addedBook);

            //Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }
        //
        //AJAX: /ShppingCart/RemoveFromCart/5
        [HttpPost]
        public ActionResult RemoveFromCart(int id) 
        {
            //Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            //Get the name of the book to display confirmation
            string bookName = storeDB.Carts
                .Single(item => item.RecordId == id).Book.BookName;

            //Remove from cart
            int itemCount = cart.RemoveFromCart(id);

            //Display the confirmation message
            var results = new ShoppingCartRemoveViewModel
            {
                Message = Server.HtmlEncode(bookName) + "has been removed from your shopping cart",
                CartTotal = cart.GetTotal(),
                ItemCount = itemCount,
                DeleteId = id
            };
            return Json(results);

        }
        //
        //GET:/ShoppingCart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary() 
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();
            return PartialView("CartSummary");
        }


    }
}
